package com.studyv3.test.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.studyv3.common.core.domain.PageBean;
import com.studyv3.common.core.domain.R;
import com.studyv3.common.utils.SecurityUtils;
import com.studyv3.test.domain.ApplyTake;
import com.studyv3.test.domain.dto.ApplyTakeDto;
import com.studyv3.test.domain.dto.ApplyTakeExport;
import com.studyv3.test.domain.dto.ApplyTakeImport;
import com.studyv3.test.domain.vo.ApplyTakeVo;
import com.studyv3.test.domain.vo.ApplyTakeWhere;
import com.studyv3.test.mapper.ApplyTakeMapper;
import com.studyv3.test.service.ApplyTakeService;
import lombok.val;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * lb_领取申请/审批(ApplyTake)表服务实现类
 *
 * @author makejava
 * @since 2024-05-06 18:48:18
 */
@Service("applyTakeService")
public class ApplyTakeServiceImpl implements ApplyTakeService {
    @Resource
    private ApplyTakeMapper applyTakeDao;

    @Override
    public R queryById(Integer id) {
        ApplyTakeDto applyTake = applyTakeDao.queryById(id);
        if(applyTake != null){
            return R.ok(applyTake,"查询成功");
        }
        return R.fail("查询失败");
    }

    @Transactional
    @Override
    public R queryByPage(ApplyTakeWhere applyTakeWhere) {
        System.out.println("=======================");
        System.out.println(applyTakeWhere);
        applyTakeWhere.setPageNumber((applyTakeWhere.getPageNumber()-1)*applyTakeWhere.getPageSize());
        applyTakeWhere.setUserid(SecurityUtils.getUserId());
        List<ApplyTakeDto> applyTakes = applyTakeDao.queryAllByLimit(applyTakeWhere);
//        if(applyTakes.size()>0){
//            PageBean<ApplyTakeDto> pageResult = new PageBean<>();
//            pageResult.setTotal(applyTakeDao.selectPageCount(applyTakeWhere));
//            pageResult.setItems(applyTakes);
//            return R.ok(pageResult,"查询成功");
//        }else{
//            return R.ok();
//        }
        PageBean<ApplyTakeDto> pageResult = new PageBean<>();
        pageResult.setTotal(applyTakeDao.selectPageCount(applyTakeWhere));
        pageResult.setItems(applyTakes);
        return R.ok(pageResult,"查询成功");
    }

    @Override
    public R queryByUnPass(ApplyTakeWhere applyTakeWhere) {
        applyTakeWhere.setPageNumber((applyTakeWhere.getPageNumber()-1)*applyTakeWhere.getPageSize());
        List<ApplyTakeDto> applyTakes = applyTakeDao.queryAllUnPass(applyTakeWhere);
        PageBean<ApplyTakeDto> pageResult = new PageBean<>();
        pageResult.setTotal(applyTakeDao.selectUnApproveCount(applyTakeWhere));
        pageResult.setItems(applyTakes);
        return R.ok(pageResult,"查询成功");
    }

    @Transactional
    @Override
    public R insert(ApplyTakeVo applyTakeVo) {
        //申请的物品数量小于库存数量时，给出提示
        if(applyTakeDao.selectStockByProductId(applyTakeVo) < Integer.parseInt(applyTakeVo.getAmount())){
            return R.fail("库存余量不足");
        }
        applyTakeVo.setName(SecurityUtils.getUsername());
        applyTakeVo.setUserid(SecurityUtils.getUserId());
        applyTakeVo.setDeleted(0);
        applyTakeVo.setCreateTime(new Date());
        applyTakeVo.setFlag(1);
        if(applyTakeDao.insert(applyTakeVo)>0){
            //修改库存表的申请物品数量
            if(applyTakeDao.addProductsApply(applyTakeVo)>0){
                return R.ok("新增申请成功");
            }else {
                return R.fail("修改库存申请物品数量失败");
            }

        }
        return R.fail("新增申请失败");
    }

    @Transactional
    @Override
    public R update(ApplyTakeVo applyTakeVo) {
        System.err.println(applyTakeVo);

        //申请的物品数量小于库存数量时，给出提示
        if(applyTakeDao.selectStockByProductId(applyTakeVo) < Integer.parseInt(applyTakeVo.getAmount())
                && applyTakeVo.getAbc()!=1){
            return R.fail("库存余量不足");
        }
        ApplyTake applyTake = BeanUtil.copyProperties(applyTakeVo, ApplyTake.class);
        applyTake.setUpdateTime(new Date());
        if(applyTakeDao.update(applyTake)>0){
            //如果是撤回或退回，修改库存表的申请物品数量
            if(applyTake.getFlag() == 2 || applyTake.getFlag() == 3){
                if(applyTakeDao.reduceProductsApply(applyTakeVo)>0){
                    return R.ok("修改申请成功");
                }else {
                    return R.fail("修改库存申请物品数量失败");
                }
            }
            applyTakeDao.addProductsApply(applyTakeVo);
            return R.ok("修改成功");
        }
        return R.fail("修改失败");
    }

    @Override
    public R deleteById(Integer id) {
        ApplyTake applyTake = new ApplyTake();
        applyTake.setDeleted(1);
        applyTake.setId(id);
        if(applyTakeDao.update(applyTake)>0){
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }

    @Transactional
    @Override
    public R deleteByIds(Integer[] ids) {
        for (int id:ids) {
            deleteById(id);
        }
        return R.ok("删除成功");
    }

    @Transactional
    @Override
    public R pass(ApplyTakeVo applyTakeVo) {
        //领用申请通过，修改库存表的库存数量，并且修改申请数量
        applyTakeVo.setApprover(SecurityUtils.getUsername());
        applyTakeVo.setPassTime(new Date());
        ApplyTake applyTake = BeanUtil.copyProperties(applyTakeVo, ApplyTake.class);
        if(applyTakeDao.updateById(applyTake)>0){
            if(applyTakeDao.pass(applyTakeVo)>0 && applyTakeDao.reduceProductsApply(applyTakeVo)>0){
                return R.ok("已通过");
            }else{
                R.fail("修改库存失败");
            }
        }
        return R.fail("通过失败");
    }

    @Transactional
    @Override
    public R importExcel(InputStream stream) {
        List<ApplyTakeImport> userExcels = EasyExcel.read(stream, ApplyTakeImport.class, null).sheet().doReadSync();
        List<ApplyTakeImport> result = new ArrayList<>();
        userExcels.forEach(u->{
            ApplyTakeVo ucopy = new ApplyTakeVo();
            ucopy.setAmount(u.getAmount());
            ucopy.setName(SecurityUtils.getUsername());
            ucopy.setUserid(SecurityUtils.getUserId());
            ucopy.setBatchid(applyTakeDao.getBatchid(u));
            ucopy.setProduct(applyTakeDao.getProductId(u));
            ucopy.setCreateTime(new Date());
            ucopy.setFlag(1);
            ucopy.setDeleted(0);
            if(applyTakeDao.insert(ucopy)>0){
            }else{
                result.add(u);
            }
        });
        if(result.size()==0){
            return R.ok("导入成功");
        }else {
            return R.fail("导入失败");
        }
    }

    @Override
    public void exportExcel(OutputStream stream, ApplyTakeWhere applyTakeWhere) {
        //查询
        List<ApplyTakeExport> list=applyTakeDao.exportQuery(applyTakeWhere);
        //生成
        EasyExcel.write(stream,ApplyTakeExport.class).sheet("领用申请信息").doWrite(list);
    }

    @Override
    public Long getCount(ApplyTakeWhere applyTakeWhere) {
        return applyTakeDao.selectPageCount(applyTakeWhere);
    }

    @Transactional
    @Override
    public R passbatch(Integer[] ids) {
        for (Integer id:ids) {
            ApplyTakeDto dto = applyTakeDao.queryById(id);
            ApplyTakeVo applyTake = BeanUtil.copyProperties(dto,ApplyTakeVo.class);;
            applyTake.setFlag(0);
            pass(applyTake);
        }
        return R.ok("批量同意成功");
    }


}
